package com.jinyi.wwdeng.ui.main.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.ui.main.contract.CreditAuditContract
import com.jinyi.wwdeng.ui.main.contract.CreditRepaymentContract
import org.json.JSONArray
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:21
 * @Annotation：Presenter 实现类
 */
class CreditRepaymentPresenterImpl: BasePresenter<CreditRepaymentContract.View,CreditRepaymentModelImpl>(),CreditRepaymentContract.Presenter {

    override fun createModel(): CreditRepaymentModelImpl {
        return CreditRepaymentModelImpl(this)
    }

    /**
     * 获取用户还款信息
     */
    fun loadCreditRepaymentDataRequest(){
        if (isViewDestory()) {
            return
        }
        model!!.loadRepaymentDataRequest()
    }

    /**
     * 获取还款Url
     */
    fun loadCreditRepaymentUrlRequest(){
        if (isViewDestory()) {
            return
        }
        model!!.loadRepaymentUrlRequest()
    }

    /**
     * 获取还款信息成功
     */
    override fun onRepaymentDataSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when(data.optInt("code")){
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                //某些地区没有repayList，需要手动添加数据
                if (!obj!!.has("repayList") || obj.optJSONArray("repayList") == null){
                    val unit = obj.optString("currency")
                    val repayArray = JSONArray()
                    val amountItem = JSONObject()
                    amountItem.put("showName",context!!.getString(R.string.repaylist_loan_amount))
                    amountItem.put("showDetail",obj.optString("amount") + unit)
                    repayArray.put(amountItem)

                    val interestItem = JSONObject()
                    interestItem.put("showName",context!!.getString(R.string.repaylist_interest))
                    interestItem.put("showDetail",obj.optString("interest") + unit)
                    repayArray.put(interestItem)

                    val serviceFeeItem = JSONObject()
                    serviceFeeItem.put("showName",context!!.getString(R.string.repaylist_service_fee))
                    serviceFeeItem.put("showDetail",obj.optString("serviceFee") + unit)
                    repayArray.put(serviceFeeItem)

                    val overdueDayItem = JSONObject()
                    overdueDayItem.put("showName",context!!.getString(R.string.repaylist_overdue_days))
                    overdueDayItem.put("showDetail",String.format(context!!.getString(R.string.repaylist_days),obj.optString("overdueDay")))
                    repayArray.put(overdueDayItem)

                    val overduePenaltyItem = JSONObject()
                    overduePenaltyItem.put("showName",context!!.getString(R.string.repaylist_overdue_penalty))
                    overduePenaltyItem.put("showDetail",obj.optString("overdueAmount") + unit)
                    repayArray.put(overduePenaltyItem)
                    obj.put("repayList",repayArray)
                }
                mvpView!!.showRepaymentData(obj)
            }
            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 获取还款url成功
     */
    override fun onRepaymentUrlSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when(data.optInt("code")){
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                val repayUrl = obj.optString("repayUrl")
                val url =obj.optString("url")
                mvpView!!.setRepaymentUrl(repayUrl)
            }
            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    override fun onRequestFinish() {
        mvpView!!.onRequestFinish()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}